﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PiaoanWeb.AppLogic.BusinessLogicLayer;
using PiaoanWeb.AppLogic.DataAccessLayer;
using PiaoanWeb.Helpers;
namespace PiaoanWeb.Admin
{
    public partial class ProductList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ProductsBLL productsBLL = new ProductsBLL();
                ProductsListGridView.DataSource = productsBLL.GetAllProducts();
                ProductsListGridView.DataBind();
            }
        }

        protected void addProductBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddProduct.aspx");
        }

        protected void ProductsListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label indexLbl = e.Row.FindControl("indexLbl") as Label;
                indexLbl.Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void deleteLnkBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ProductsBLL productsBLL = new ProductsBLL();
                LinkButton deleteLnkBtn = sender as LinkButton;
                productsBLL.DeleteProductsByID(Convert.ToInt32(deleteLnkBtn.CommandArgument));

                ProductsListGridView.DataSource = productsBLL.GetAllProducts();
                ProductsListGridView.DataBind();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        protected void ProductsListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ProductsListGridView.PageIndex = e.NewPageIndex;
            ProductsBLL productsBLL = new ProductsBLL();
            ProductsListGridView.DataSource = productsBLL.GetAllProducts();
            ProductsListGridView.DataBind();
        }
    }
}